package com.chuangke.file.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.chuangke.common.constant.ElasticIndexConstants;
import com.chuangke.common.http.HttpResult;
import com.chuangke.elasticsearch.model.ElasticQuery;
import com.chuangke.elasticsearch.service.ElasticsearchService;
import com.chuangke.file.model.FileDocument;
import com.chuangke.file.service.FileSearchService;

@RestController
@RequestMapping("filesearch")
public class FileElasticController {

//	@Autowired
//	private ElasticsearchService baseElasticService;
	@Autowired
	private FileSearchService fileSearchService;

//	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping("/initIndex")
	public HttpResult initIndex() {
//		if (baseElasticService.isExistsIndex(ElasticIndexConstants.FILE_INDEX)) {
//			return HttpResult.error("索引已经存在，不允许创建");
//		}
//
//		baseElasticService.createIndex(ElasticIndexConstants.FILE_INDEX);
		return HttpResult.ok();
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/query")
	public HttpResult search(@RequestBody ElasticQuery<FileDocument> elasticQuery) {
		elasticQuery.setIdxName(ElasticIndexConstants.FILE_INDEX);
//		elasticQuery.setPageNum(0);
//		elasticQuery.setPageSize(10);
		return HttpResult.ok(fileSearchService.search(elasticQuery));
	}

}
